<?php

namespace App\Http\Controllers\Api;


use App\Models\Member;
use App\Models\Sms;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Validate\api\UserValidate;
use Illuminate\Support\Facades\Hash;

class MemberController extends BaseController
{
    protected $Member;
    protected $v;
    protected $Sms;
    /**
     * api注册
     * @param Request $request
     * @return array|\Illuminate\Http\JsonResponse
     */

    public function __construct(Member $Member,UserValidate $userValidate,Sms $Sms)
    {
        $this->Member = $Member;
        $this->v = $userValidate;
        $this->Sms = $Sms;
    }


    /**
     * 忘记密码
     * @param Request $request
     * @return array|\Illuminate\Http\JsonResponse
     */
    public function forgetPassword(Request $request){
        $this->v->validate('register');
        $data=$request->input();
        $phone = $data['phone'];


        $rs =  $this->Member->checkPhone($request->phone);
        if (!$rs){
            return response()->json(['code'=>0,'msg'=>'该手机号码没有注册，请先注册']);
        }
        $verifycode = $data['verifycode'];
        if(!$this->Sms->checkCode($verifycode,$phone,2)){
            return ['code'=>0,'msg'=>'验证码错误或已到期'];
        }

        $result = $this->Member->where(['phone'=>$request->phone])->update([
            'password'=>bcrypt($request->password),
            'updated_at'=>time()
        ]);

        if ($result){
            return response()->json(['code'=>1,'msg'=>'操作成功']);
        }
        return response()->json(['code'=>0,'msg'=>'系统错误，请稍后再试']);
    }

    /**
     * 修改个人信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeInformation(Request $request){
        /*
         * nickname 昵称
         * head_pic 头像
         */


        $data = $request->all();
        if (!empty($data['nickname'])) {
            $up['nickname']=$data['nickname'];
        }
        if (!empty($data['head_pic'])) {
            $up['head_pic']=$data['head_pic'];
        }
        $up['updated_at']=time();
        $rs = Member::where(['member_id'=>Auth::user()->member_id])->update($up);
        if ($rs){
            return response()->json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'操作失败']);

        }
    }

    /**
     * 修改密码
     * @param Request $request
     * @return array
     */
    public function changePassword(Request $request){
        $this->v->validate('changePassword');
        //验证旧密码
        $oldpassword = $request->oldPassword;
        if (!Hash::check($oldpassword,Auth::user()->password)){
            return response()->json(['code'=>0,'msg'=>'旧密码错误']);
        }

        $rs=$this->Member->where('member_id',Auth::user()->member_id)->update(['password'=>bcrypt($request->newPassword)]);
        if ($rs){
            Auth::guard('api')->user()->token()->delete();
            return response()->json(['code'=>1,'msg'=>'修改成功']);
        }else{

            return response()->json(['code'=>0,'msg'=>'系统错误，请稍后再试']);
        }


    }


}
